import { ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import { sensitiveApplicationDetailApi } from '/@/api/sensitiveData/informationEntry';
import { getRouterParams } from '/@/utils/hyFunc';
import { defaultValue } from '../utils/default';
import { ElLoading } from 'element-plus';
export function useCounter() {
	const route = useRoute();
	//单位基本信息
	const basicInfoData: any = ref(defaultValue().basicInfoDefault);
	//设计单位基本信息
	const designUnitInfoData: any = ref(defaultValue().designUnitInfo);
	//应用信息
	const applyInfoData: any = ref(defaultValue().applyInfoDefault);
	//路由参数
	const routerParams: any = ref({});
	//设置标题
	const title = ref('');
	const setTtile = (type: any) => {
		if (type === 'add') {
			title.value = '添加';
		}
		if (type === 'edit') {
			title.value = '编辑';
		}
	};

	//处理数据
	const setData = (res: any) => {
		basicInfoData.value = {
			sensitiveId: res?.sensitiveId || '',
			responsibleDept: res?.responsibleDept || '', //责任部门
			responsibleDeptId: res?.responsibleDeptId?.split(',') || '', //责任部门id
			budgetUnit: res?.budgetUnit || '', //预算单位
			budgetUnitId: res?.budgetUnitId?.split(',') || '', //预算单位
			leadDept: res?.leadDept || '', //牵头部门
			leadDeptId: res?.leadDeptId?.split(',') || '', //牵头部门ID
		};
		designUnitInfoData.value = {
			socialCode: res?.socialCode || '', //信用代码
			designUnit: res?.designUnit || '', //单位名称
		};
		applyInfoData.value = {
			appName: res?.appName || '', //应用名称
			principalName: res?.principalName || '', //负责人
			principalPhone: res?.principalPhone || '', //负责人电话
			appConstructionType: res?.appConstructionType || '', //应用建设类型 1开发应用 2 运维应用
			projectType: res?.projectType || '', //应用建设类型：1-新建，2-续建，3-首次运维，4-连续运维
			budgetYear: res?.budgetYear || '', //应用预算年度
			developBeginDate: res?.developBeginDate || '', //应用开发起始时间
			developEndDate: res?.developEndDate || '', //应用开发终止时间
			domain: res?.domain || '', //应用领域 1数字党建、2数字政务、3数字经济、4数字社会、5数字文化、6数字法治、7数字智治
			yearBudget: res?.yearBudget || '', //应用年度预算（万元）
			totalBudget: res?.totalBudget || '', //应用总投资(万元)
			fundResource: res?.fundResource?.split(',') || [], //	资金来源(逗号拼接多选)：1-中央财政资金，2-市级财政资金，3-区县财政资金，4-部门自筹资金，5-银行贷款等投资，6-其他
			otherFund: res?.otherFund || '', //其他资金来源名称
			centerAmount: res?.centerAmount || '', //中央财政资金万元
			cityAmount: res?.cityAmount || '', //市级财政资金(万元)
			districtAmount: res?.districtAmount || '', //区县财政资金(万元)
			deptAmount: res?.deptAmount || '', //部门自筹资金(万元)
			bankAmount: res?.bankAmount || '', //银行自筹资金(万元)
			otherAmount: res?.otherAmount || '', //其他(万元)
			hierarchy: res?.hierarchy?.split(',') || [], //建设层级 1国家、2省级、3市级、4区县、5镇街
			mode: res?.mode?.split(',') || [], //建设模式 1统一开发、共同使用； 2统一开发、分级部署； 3统筹规划、分级开发
			localization:res?.localization || '', //是否国产化 0否1是
		};

		return {
			basicInfoData,
			designUnitInfoData,
			applyInfoData,
		};
	};
	//获取详情
	const getDetail = (sensitiveId: any) => {
		let params;
		if (!sensitiveId) return;
		params = {
			sensitiveId,
		};
		const loading = ElLoading.service({
			lock: true,
			text: '获取数据中...',
		});
		const detailRequest = sensitiveApplicationDetailApi;
		detailRequest(params)
			.then((res: any) => {
				// console.log(res);
				// debugger;
				setData(res);
				// setFlowDataShow();
			})
			.finally(() => loading.close());
	};
	watch(
		[() => route.query],
		() => {
			routerParams.value = getRouterParams();
			getDetail(routerParams.value?.sensitiveId);
			setTtile(routerParams.value?.operateType);
		},
		{ immediate: true, deep: true }
	);
	return {
		title,
		routerParams,
		basicInfoData,
		designUnitInfoData,
		applyInfoData,
	};
}
